########## Code for image analysis used int "Rapid propagation of Membrane Tension at a Retinal Bipolar Terminal" ###############

##################################
### Prerequisites:
##################################
# Bio-Formats Matlab libraries must be in matlab path https://docs.openmicroscopy.org/bio-formats/5.3.4/users/matlab/index.html 
# Matlab Image Processing, Signal Processing, and Computer Vision Toolboxes 

########################################
# Data Analysis and Plotting Functions
#########################################

### TetherSlidingAnalysis.m: Analyzes tether sliding data, calculating the force on the membrane, and tether velocity
    Requires:
	.tiff files of sliding experiments with bead, and center with bead  not under force
	.tiff file of sliding experiment regiestered in the frame of the membrane
	"snakes" file generated by JFilament tracking of points on the membrane edge (from registered images)
	Location of tether base at each time point (load as "tetherBase" in workspace)
	k_trap: trap stiffness in pN / micron (load as ktrap)
	Dbead: diameter of bead (in micron, load as Dbead)
	nmperpixel: scale of image data, in nM (load as nmperpixel)
    To-run:
	load and assemble requisite data (above)
	step through each sectioned code block:
	     Manual seps
	     1) Select bead center file, crop the image to include only the bead
	     2) Select bead under force file
	     3) select registered bead under force file, crop the image to include the bead and all positions it travels to
	     4) select the JFilament Snakes file
	     5) Run the subsequent analysis of the data
     Outputs:
	Saved workspace "Filename.mat" file containtg force, velocity, and position data
	Images as .avi and .tiff overlayed with tracking (to check quality)
	

### PlotCombinedTetherData.m: Collates multiple files generated by TetherSlidingAnalysis.m, and plots data by cell type, condition, etc. 
    Requires:
	workspaces generated by TetherSlidingAnalysis.m, sorted into directories by condition, cell-type, etc
     To-run:
	update the variable 'directory_list' at the top of the file to include the paths to the relevant data
	Run the analyses
	Plotting code should be adapated to 
     Outputs:
	Plots from figure 3,S8 displayed in the main text and supplement
	Workspaces saving all collated data from tether sliding under the relevant condition/cell-type name (i.e. 'combinedData_terminal_latA.mat')


######################################
# Auxilliary "Helper" Functions
######################################
### trackCirclePos.m: Tracks the position of a bead held in an optical trap
    Requires:
	imfile:	Name of image file with bead to track
	J : Processed and cropped image data
	DbeadMin, Dbeadmax (maximum and minimum expected bead diameter, microns)
	framespersecond  : time beteween each image slice
	nmperpix : scale of image data, in nM
     Returns:
	X,Y positions of the bead at each time point
	
### PreprocessImages.m : Loads and processes a .tiff file image
    Requires:
      full_file: file and path to .tiff file to track
    Returns:
     I2 = processed matrix with image data in x,y,t order 
     h = image height
     w = image width
     Nfr = number of frames 
     dt = time step or inverse framerate of video

### OTForceVector.m: Calculates the force a bead is under
    Requires:
	I2 = processed matrix with image data in x,y,t order 
     	h = image height
     	w = image width
     	Nfr = number of frames 
     	dt = time step or inverse framerate of video
    Returns:
        ftether - force on the tether at each time point (pN)
    	C1 - image data of bead not under force
    	delX1, delY1 - x,y deviations from center for bead not under force
    	C2, delX2, delY2 - "    " for bead under force
    	bead_position - x,y ,position ofe bead under force
    	fname - name of bead under force fiel
    	dt2 - timestep for bead under foce
    	fullf2 - full path to bead not under force

### trackMembraneEdgeRegistered.m: Tracks the edge of a membrane using points generated by JFilament. Calls trackCirclePos.m to track the position of the registered bead
    inputs:
	Dbead = Diamter of bead (microns)
    	nmperpix = scale of image (nM)
     Requires:
	snakes file tracking membrane edge generated by Jfilament
	bead-under-force images registered in the frame of the membrane
     Returns:
	snake_array = 2xNsnakexNframes array of x,y, positions of the snakes where Nsnake is the number of points tracked
	imfile=name of image file
	bead_pos_reg = position of the bead registered t ofhe frame of the membrane
